import { ref } from 'vue'
import { ossConfig } from '@/utils/alioss.js'
import Http from '@/utils/requset'
import { useStore } from '@/store/index'
const store = useStore()
// 地图上用到的头像
export function creatMapHeader () {
  const imgHeader = ref('')
  // 画头像 defaultAvatar 相对路径和全路径都可
  const drawHeader = (gender, defaultAvatar, cb) => {
    const ctx = uni.createCanvasContext('firstCanvas')
    console.log('开始上传头像了...')
    console.log(`https://img.qumoyugo.com/webimgbg/pico-map-header-bg${gender}.png`)
    uni.downloadFile({
      url: `https://img.qumoyugo.com/webimgbg/pico-map-header-bg${gender}.png`,
      success: (res) => {
        if (res.statusCode === 200) {
          const _defaultAvatar = defaultAvatar.indexOf('https') === -1 ? `https://img.qumoyugo.com${defaultAvatar}` : defaultAvatar
          uni.downloadFile({
            url: _defaultAvatar,
            success: (res2) => {
              ctx.drawImage(res.tempFilePath, 0, 0)
              ctx.drawImage(res2.tempFilePath, 47, 39, 109, 109)
              // 画圆环
              ctx.setLineWidth(22)
              ctx.setStrokeStyle(gender === 1 ? '#525FA2' : '#ea742b')
              // ctx.setStrokeStyle('#f00')
              ctx.setLineCap('round')
              ctx.beginPath()
              ctx.arc(101, 94, 66, 0, 2 * Math.PI, false)
              ctx.stroke()
              ctx.draw(false, () => {
                uni.canvasToTempFilePath({
                  canvasId: 'firstCanvas',
                  success: (res4) => {
                    ossConfig(res4.tempFilePath).then(res5 => {
                      imgHeader.value = res5
                      console.log('返回图片地址', res5)
                      cb && cb(res5)
                    })
                  }
                })
              })
            }
          })
        }
      },
      fail: () => {
        uni.showToast({
          title: '网络错误'
        })
      }
    })
  }
  return {
    imgHeader,
    drawHeader
  }
}
// 添加黑名单
export function addPullBlack (id, callback) {
  Http({
    url: `/blacklist/${id}`,
    type: 'pp',
    method: 'post'
  }).then(res => {
    if (res.code === 0) {
      uni.showToast({
        title: '操作成功',
        icon: 'none',
        success: () => {
          callback && callback()
        }
      })
    }
  })
}
// 消息数量获取
export function getMessageNum () {
  Http({
    url: '/center/message/num',
    type: 'm',
    data: {
      jump: 'false'
    }
  }).then(res => {
    if (res.code === 0) {
      store.commit('setMessageNum', res.data)
    }
  })
}
